package com.huishen.edrivenew.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.huishen.edrivenew.AppContext;
import com.huishen.edrivenew.Constants;
import com.huishen.edrivenew.R;
import com.huishen.edrivenew.base.BaseActivity;
import com.huishen.edrivenew.bean.StudentBean;
import com.huishen.edrivenew.net.JsonResponseHandler;
import com.huishen.edrivenew.net.NetApi;
import com.huishen.edrivenew.util.StringUtils;

/**
 * 登录页面
 */
public class LoginActivity extends BaseActivity {
    
    @InjectView(R.id.verify_edit_number)
    public EditText userName;
    
    @InjectView(R.id.verify_edit_vcode)
    public EditText passWord;
    
    @InjectView(R.id.verify_btn_start)
    public Button loginBtn;
    
    @InjectView(R.id.changePassword)
    public TextView changePwd;
    
    private String t_username;
    private String t_password;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_login);
        ButterKnife.inject(this);
        
        if(AppContext.getInstance().getLogin()) {
            toMain();
        } else {
          //重置flag
            AppContext.getInstance().resetAppId();
        }
        
        String username = AppContext.getInstance().getUserName();
        if(!StringUtils.isEmpty(username)) {
            userName.setText(username);
        }
        
    }
    
    
    
    @OnClick(R.id.verify_btn_start)
    public void login() {
        t_username = userName.getText().toString().trim();
        t_password = passWord.getText().toString().trim();
        
        if(StringUtils.isEmpty(t_username)) {
            showToast("请输入注册帐号");
            return;
        }
        if(StringUtils.isEmpty(t_password)) {
            showToast("请输入密码");
            return;
        }
        
        showDialog();
        loginRequest(t_username, t_password);
    }
    
    
    private void loginRequest(final String uName, String pwd) {
        NetApi.login(this, new JsonResponseHandler() {
            @Override
            public void onSuccess(int code, JSONObject jsonObj, Object[] args) {
                dismissDialog();
                try {
                    
                    if(code == Constants.N_PWD_ERROR) {
                        showToast("用户名或密码错误！");
                    } else if(code == Constants.N_SUCCESS) {
                        StudentBean bean = (StudentBean) StudentBean.parseJson(jsonObj.getJSONObject("student"));
                        JSONObject jinfo = jsonObj.getJSONObject("stuInfo").getJSONObject("stuInfo");
                        
                        bean.money = jinfo.optInt("money", 0);
                        bean.balance = jinfo.optInt("balance");
                        bean.timeNum = jinfo.optInt("timeNum");
                        AppContext.getInstance().studentBean = bean;
                        AppContext.getInstance().saveStudent(bean);
                        AppContext.getInstance().saveLogin("1");
                        
                        AppContext.getInstance().saveUserName(uName);
                        
                        toMain();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast("登录失败！");
                }
            }
            @Override
            public void onFailure(int code, String errorMessage, Object[] args) {
                dismissDialog();
                showToast("登录失败！");
            }
        }, uName, pwd);
    }
    
    
    private void toMain() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.startActivity(intent);
        this.finish();
    }
    
    @OnClick(R.id.changePassword)
    public void toChangePwd() {
        Intent intent = new Intent(this, ChangePwdActivity.class);
        this.startActivity(intent);
    }
    
}
